package com.zhongxi.guoyuan.adapter.seller;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.seller.SellerMallOrderResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/13.
 */

public class SellerExchangeAdapter extends BaseAdapter {

    private Context context;
    private List<SellerMallOrderResult.OrderInfoBean> list;
    private SellerMallOrderResult.OrderInfoBean infoBean;

    public SellerExchangeAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List<SellerMallOrderResult.OrderInfoBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            holder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.seller_exchange_layout, parent, false);
            holder.seller_exchangeitem_image = (ImageView) convertView.findViewById(R.id.seller_exchangeitem_image);
            holder.seller_exchangeitem_hasConsume = (TextView) convertView.findViewById(R.id.seller_exchangeitem_hasConsume);
            holder.seller_exchangeitem_name = (TextView) convertView.findViewById(R.id.seller_exchangeitem_name);
            holder.seller_exchangeitem_markPrice = (TextView) convertView.findViewById(R.id.seller_exchangeitem_markPrice);
            holder.seller_exchangeitem_num = (TextView) convertView.findViewById(R.id.seller_exchangeitem_num);
            holder.seller_exchangeitem_realPrice = (TextView) convertView.findViewById(R.id.seller_exchangeitem_realPrice);
            holder.seller_exchangeitem_customerPhone = (TextView) convertView.findViewById(R.id.seller_exchangeitem_customerPhone);
            holder.seller_exchangeitem_call = (TextView) convertView.findViewById(R.id.seller_exchangeitem_call);
            holder.seller_exchangeitem_orderaNumber = (TextView) convertView.findViewById(R.id.seller_exchangeitem_orderaNumber);
            holder.seller_exchangeitem_payTime = (TextView) convertView.findViewById(R.id.seller_exchangeitem_payTime);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }

        if (null != list && !"".equals(list)) {
            infoBean = list.get(position);

            Glide.with(context).load(Url.IMG_URL + infoBean.getGoodsimage()).placeholder(R.mipmap.iv_eg).into(holder.seller_exchangeitem_image);

            if (null != infoBean.getGoodsname() && !"".equals(infoBean.getGoodsname())) {
                holder.seller_exchangeitem_name.setText(infoBean.getGoodsname());
            } else {
                holder.seller_exchangeitem_name.setText("");
            }

            if (null != infoBean.getGoodsunitprice() && !"".equals(infoBean.getGoodsunitprice())) {
                holder.seller_exchangeitem_markPrice.setText(infoBean.getGoodsunitprice() + "美豆");
            } else {
                holder.seller_exchangeitem_markPrice.setText("");
            }
            if (null != infoBean.getGoodsnum() && !"".equals(infoBean.getGoodsnum())) {
                holder.seller_exchangeitem_num.setText("数量：" + infoBean.getGoodsnum());
            } else {
                holder.seller_exchangeitem_num.setText("数量：");
            }

            if (null != infoBean.getPrice() && !"".equals(infoBean.getPrice())) {
                holder.seller_exchangeitem_realPrice.setText(infoBean.getPrice() + "美豆");
            } else {
                holder.seller_exchangeitem_realPrice.setText("");
            }

            if (null != infoBean.getUserphone() && !"".equals(infoBean.getUserphone())) {
                holder.seller_exchangeitem_customerPhone.setText("顾客：" + infoBean.getUserphone());
                holder.seller_exchangeitem_call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + list.get(position).getUserphone());
                        intent.setData(data);
                        context.startActivity(intent);
                    }
                });
            } else {
                holder.seller_exchangeitem_customerPhone.setText("顾客：");
                holder.seller_exchangeitem_call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "号码无效", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }

        if (null != infoBean.getId() && !"".equals(infoBean.getId())) {
            holder.seller_exchangeitem_orderaNumber.setText("订单编号：" + infoBean.getId());
        } else {
            holder.seller_exchangeitem_orderaNumber.setText("订单编号：");
        }

        if (null != infoBean.getCtime() && !"".equals(infoBean.getCtime())) {
            holder.seller_exchangeitem_payTime.setText("支付时间：" + infoBean.getCtime());
        } else {
            holder.seller_exchangeitem_payTime.setText("支付时间：");
        }
        return convertView;
    }


    static class MyViewHolder {

        private ImageView seller_exchangeitem_image;
        private TextView seller_exchangeitem_hasConsume;
        private TextView seller_exchangeitem_name;
        private TextView seller_exchangeitem_markPrice;
        private TextView seller_exchangeitem_num;
        private TextView seller_exchangeitem_realPrice;
        private TextView seller_exchangeitem_customerPhone;
        private TextView seller_exchangeitem_call;
        private TextView seller_exchangeitem_orderaNumber;
        private TextView seller_exchangeitem_payTime;

    }
}
